<?php


namespace Home\Controller;


class CaseController extends PublicController
{

    function index(){
        $id = I('get.id');

        $page = I('post.page');
        $pagesize = 50;

        if ($page == null) {
            $page = 1;
        }

        $where = "a.status = 2 and a.is_show = 1";

        if ($id != "") {
            $where .= " and a.user_id = ".$id;
        }
        $type_id = I('post.type_id');
        if ($type_id!=""&&$type_id!=0&&$type_id!=null) {
            $where .= " and a.type_id = ".$type_id;
        }
        $find_type=I('post.find_type');
        $search_like = I('post.search_like');
        if($find_type == 1 && $search_like != ""){
            $where.=' and a.name like "%'.$search_like.'%"';
        }
        $active_name=I('post.active_name');
        $order='id asc';
        if($active_name == 2){
            $order='sell_number desc';
        }else if($active_name == 3){
            //评分暂无
        }
        $data = M('goods as a')
            ->join('left join type as b on a.type_id = b.id')->where($where)->field('a.* , b.name as type_name')->order($order)->page($page,$pagesize)->select();
        $data = D('Goods')->parse_goods($data);
        $this->data=$data;
        $this->display();
    }

    function caselist(){
        $good_id = I('get.good_id');
        $data1 = M('content as a')
            ->join('left join user as b on a.user_id = b.id left join city as c on b.city_id = c.id')
            ->field('a.* ,b.exp, b.nick_name , b.head_ico , c.name as city_name , b.major ,b.school_end_time')
            ->where('a.status = 1 and  a.object_id = '.$good_id.' and a.class_id = 11')
            ->order('create_time desc')
            ->select();
        $data2 = M('shop_order as a')
            ->join('left join content as b on a.results_id = b.id ')
            ->field('b.*')
            ->where('a.status = 5 and  a.good_id = '.$good_id.' and a.results_id != 0 and b.status = 1')
            ->order('create_time desc')
            ->select();
        $data = array_merge($data1,$data2);

        foreach ($data as $key=>$value){
            $data[$key]['create_time']=friendlyDate($value['create_time']);
            $data[$key]['files']=json_decode($data[$key]['files']);
            if(!empty($value['order_no'])){
                $data[$key]['order_comment_info']=M('score as a')
                    ->join('left join user as b on a.user_id=b.id left join user as c on a.store_id = c.id')
                    ->field('a.*,b.head_ico as user_head_ico,b.nick_name as user_nick_name,b.real_name as user_real_name ,c.head_ico as shop_head_ico,c.nick_name as shop_nick_name,c.real_name as shop_real_name')
                    ->where('a.order_no="'.$value['order_no'].'"')->select();
                foreach ($data[$key]['order_comment_info'] as $k1=>$v2){
                    $data[$key]['order_comment_info'][$k1]['time_str']=friendlyDate($v2['time']);
                }

            }else{
                $data[$key]['order_comment_info']=[];
            }
        }
        //$data = D('content')->parse_contents($data);

        $this->data=$data;
        $this->display();
    }

    function caseinfo(){
        $good_id = I('get.good_id');
        $data = M('content as a')
            ->where('a.status = 1 and  a.object_id = '.$good_id.' and a.class_id = 11')
            ->order('create_time desc')
            ->find();


        $this->data=$data;
        $this->display();
    }

}